Page({
  data: {
    id: '',
    houseInfo: '',
    url: '',
    validTime: 0,
    // 后台准备的加密信息，用于访客正常访问
    encryptedData: ''
  },
  onLoad ({ id, encryptedData }) {
    if (encryptedData) {
      this.getEncryptedData(encryptedData)
    } else {
      this.getDetail(id)
    }
  },
  // 当倒计时结束时
  onFinish () {
    this.setData({ validTime: 0 })
  },
  // 获取页面列表数据
  async getDetail (id) {
    const res = await wx.http({ url: `/visitor/${id}` })
    this.setData({ ...res.data })
  },
  async getEncryptedData (encryptedData) {
    const res = await wx.http({ url: `/visitor/share/${encryptedData}` })
    this.setData({ ...res.data })
  },
  onShareAppMessage () {
    return {
      title: '快看快看',
      path: '/visitor_pkg/pages/passport/index?encryptedData=' + this.data.encryptedData,
      imageUrl: 'https://p.qqan.com/up/2023-3/16799624321042127.jpg'
    }
  },
  // 保存图片
  onSave () {
    wx.downloadFile({
      url: 'https://p.qqan.com/up/2023-3/16799624321042127.jpg',
      success: res => {
        // console.log(res)
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: () => {
            wx.showToast({ title: '保存成功' })
          }
        })
      }
    })
  }
})
